<script>
import basepath from '../../plugins/base.js'
export default {
  data() {
    return {
      baseimg: '',
      baseapi: '',
      form: {
        loginName: '',
        loginPwd: '',
        phone: '',
        status: true,
        path: '',
        name: '',
        sex: '',
        roleName: ''
      }
    }
  },
  methods: {
    onSubmit() {

    }

    //上传成功
    , handleAvatarSuccess(res, file) {
      console.log('file: ', file);
      console.log('res: ', res);
      if (res.code == 1) {
        this.$message.success('头像上传成功!');
        this.form.path = res.filepath;
      } else {
        this.$message.error(res.msg);
      }
    },

    //上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';

      const isLtMB = file.size / 1024 / 1024 < 10;

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 和 PNG 格式!');
      }
      if (!isLtMB) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return (isJPG || isPNG) && isLtMB;
    }
  },
  created() {
    this.baseimg = basepath.imgurl;
    this.baseapi = basepath.apiurl;
    this.axios({
      method: 'get',
      url: 'Users/info'
    }).then(response => {
      this.form = response;
    })
  }
}
</script>

<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px" style="width: 500px;">
      <el-form-item label="账户">
        <el-input v-model="form.loginName" disabled></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-col :span="16" :offset="0">
          <el-input value="******" disabled></el-input>
        </el-col>
        <el-col :span="6" :offset="2">
          <el-button @click="onSubmit">修改密码</el-button>
        </el-col>

      </el-form-item>
      <el-form-item label="手机">
        <el-input v-model="form.phone" disabled></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch disabled active-color="#13ce66" inactive-color="#ff4949"
          v-model="form.status"></el-switch>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="form.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="form.sex" disabled></el-input>
      </el-form-item>
      <el-form-item label="头像">
        <el-upload class="avatar-uploader" :action="baseapi+'/Files'"
          :show-file-list="false" :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.path" :src="baseimg+form.path" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="角色">
        <el-input v-model="form.roleName" disabled></el-input>
      </el-form-item>
    </el-form>

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>